package org.tea.inventory.db.service;

import com.github.pagehelper.PageHelper;
import org.tea.inventory.db.dao.DemandSensitiveWordMapper;
import org.tea.inventory.db.dao.NewsCommentMapper;
import org.tea.inventory.db.domain.NewsComment;
import org.tea.inventory.db.util.BadWordUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Service
public class NewsCommentService {

    @Resource
    private NewsCommentMapper newsCommentMapper;
    @Resource
    private DemandSensitiveWordMapper demandSensitiveWordMapper;

    /**
     *  获取评论列表
     */
    public List<NewsComment> querySelective(NewsComment newsComment, int page, int limit, String sort) {
        List<NewsComment> lstNewsComment = null;
        PageHelper.startPage(page, limit);
        if(newsComment.getContent()!=null && !"".equals(newsComment.getContent())){
        	try {
				newsComment.setContent(URLEncoder.encode(newsComment.getContent(), "utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        lstNewsComment = newsCommentMapper.selectByExample(newsComment);
	    if(lstNewsComment != null){
	    	for(int i = 0;i<lstNewsComment.size();i++){
	    		try {
					lstNewsComment.get(i).setContent(URLDecoder.decode(lstNewsComment.get(i).getContent(), "utf-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	    }
        return lstNewsComment;
    }
    /**
     *  获取评论列表
     */
    public List<NewsComment> selectCommonList(NewsComment newsComment, int page, int limit, String sort) {

        List<NewsComment> lstNewsComment = null;
        PageHelper.startPage(page, limit);
        if(newsComment.getContent()!=null && !"".equals(newsComment.getContent())){
        	try {
				newsComment.setContent(URLEncoder.encode(newsComment.getContent(), "utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        lstNewsComment = newsCommentMapper.selectCommonList(newsComment);
        if(lstNewsComment != null){
	    	for(int i = 0;i<lstNewsComment.size();i++){
	    		try {
					lstNewsComment.get(i).setContent(URLDecoder.decode(lstNewsComment.get(i).getContent(), "utf-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	    }
        return lstNewsComment;
    }

    /**
     * 获取评论列表数量
     */
    public Integer countSelective(NewsComment newsComment) {
    	if(newsComment.getContent()!=null && !"".equals(newsComment.getContent())){
        	try {
				newsComment.setContent(URLEncoder.encode(newsComment.getContent(), "utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        int total = (int)newsCommentMapper.countByExample(newsComment);
        return total;
    }

    /**
     * 获取评论列表数量
     */
    public Integer countByComList(NewsComment newsComment) {
    	if(newsComment.getContent()!=null && !"".equals(newsComment.getContent())){
        	try {
				newsComment.setContent(URLEncoder.encode(newsComment.getContent(), "utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
        int total = (int)newsCommentMapper.countByComList(newsComment);
        return total;
    }

    /**
     * 获取指定id的评论详情
     * @param id
     * @return
     */
    public NewsComment findById(Integer id) {

        NewsComment newsComment = null;
        newsComment = newsCommentMapper.selectByPrimaryKey(id);
		if(newsComment != null){
			try {
				newsComment.setContent(URLDecoder.decode(newsComment.getContent(), "utf-8"));
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
        return newsComment;
    }

    /**
     * 录入评论信息
     * @param newsComment
     */
    public void add(NewsComment newsComment) {
    	try {
			Set<String> keyWordSet = new HashSet<String>();
			keyWordSet = demandSensitiveWordMapper.selectDemandSensitiveWord();
			String content= newsComment.getContent();
			if(keyWordSet != null && keyWordSet.size() > 0){
				BadWordUtil.addBadWordToHashMap(keyWordSet);
				//是否包含敏感词
				boolean flag = BadWordUtil.isContaintBadWord(content,2);
				if(flag){
					content = BadWordUtil.replaceBadWord(content, 2, "*");
				}
			}
    		newsComment.setContent(URLEncoder.encode(content, "utf-8"));
    		newsCommentMapper.insertSelective(newsComment);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }

    /**
     * 删除评论信息
     * @param id
     */
    public void delete(Integer id) {
        newsCommentMapper.deleteByPrimaryKey(id);
    }

    public void deleteQuantity(List<String> list){
        for (String id:list) {
            newsCommentMapper.deleteByPrimaryKey(Integer.parseInt(id));
        }
    }
    
   public List<NewsComment>  selectByZiExample(Integer commentfirstid){
	   List<NewsComment> lstNewsComment = null;
	   lstNewsComment = newsCommentMapper.selectByZiExample(commentfirstid);
	   if(lstNewsComment != null){
	    	for(int i = 0;i<lstNewsComment.size();i++){
	    		try {
					lstNewsComment.get(i).setContent(URLDecoder.decode(lstNewsComment.get(i).getContent(), "utf-8"));
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	    }
	    return lstNewsComment;
    }
}
